package net.cmkj.mine.core.admin.sys.role;

import com.jfinal.plugin.activerecord.Page;

import net.cmkj.mine.bean.SysRole;
import net.cmkj.mine.common.anno.RequiresPermissions;
import net.cmkj.mine.common.controller.BaseController;
import net.cmkj.mine.common.model.DataGridModel;
import net.cmkj.mine.util.DataGridModelUtils;

public class RoleController extends BaseController {
	
	private SysRole dao = SysRole.dao;
	
	@RequiresPermissions(value={"/sys/role"})
	public void index() {
		render("role.html");
	}
	
	@RequiresPermissions(value={"/sys/user"}) 
	public void form() throws Exception {
		Integer id=getParaToInt("id");
		if(id != null){
			setAttr("role", dao.findById(id));
		}
		render("roleForm.html");
	}
	
	@RequiresPermissions(value={"/sys/role"})
	public void getList() throws Exception {
		String name=getPara("name");
		Integer status=getParaToInt("status");
		Page<SysRole> pageInfo = dao.paginate(getPage(), getRows(), name, status, getOrderbyStr());
		DataGridModel result = DataGridModelUtils.toDataGridView(pageInfo);
		renderJson(result);
	}
	
	@RequiresPermissions(value={"/sys/role"})
	public void roleRes() throws Exception {
		Integer id=getParaToInt("id");
		if(id != null){
			setAttr("role", dao.findById(id));
			setAttr("restree", dao.getZtreeViewList(1, id));
		}
		render("roleRes.html");
	}
	
	/**
	 * 保存/修改角色信息
	 */
	@RequiresPermissions(value={"/sys/role"})
	public void save() throws Exception {
		renderJson(dao.save(getParaToInt("id"), getPara("name"), getParaToInt("seq"), getPara("des")));
	}
	
	/**
	 * 设置数据启用/禁用
	 */
	@RequiresPermissions(value={"/sys/role"})
	public void visible() throws Exception {
		Integer visible=getParaToInt("visible");
		Integer id=getParaToInt("id");
		renderJson(dao.setVisible(id, visible));
	}
	
	/**
	 * 角色绑定资源.
	 */
	@RequiresPermissions(value={"/sys/role"})
	public void saveMenu() throws Exception {
		Integer roleId=getParaToInt("roleId");
		String menuIds=getPara("menuIds");
		renderJson(dao.saveMenu(roleId, menuIds));
	}
	
	@RequiresPermissions(value={"/sys/user"})
	public void delete() throws Exception {
		renderJson(dao.deleteByIds(getPara("ids")));
	}

}
